{% block generic_label %}
{% spaceless %}
    {% if required %}
        {% set attr = attr|merge({'class': attr.class|default('') ~ ' required'}) %}
    {% endif %}
    <label class="control-label" {% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>{#% if required %}<b>*</b>{% endif %#}{{ label|trans }}</label>
{% endspaceless %}
{% endblock %}

{# custom widget - gender #}
{% block gender_widget %}
{% spaceless %}
	{% if (value) %}
	<script>
	$(function() {
		$('#{{ form.vars.id }} option').each(function() {
			if($(this).val() == {{ value }}) {
				$(this).attr("selected", "selected");
			}
		});
	});
	</script>
	{% endif %}
	{% if expanded %}
		<div {{ block('widget_container_attributes') }}>
		{% for child in form %}
			{{ form_widget(child) }}
			{{ form_label(child) }}
		{% endfor %}
		</div>
	{% else %}
        {# just let the choice widget render the select tag #}
        {{ block('choice_widget') }}
	{% endif %}
{% endspaceless %}
{% endblock %}

{# custom widget - university_selector #}
{% block university_selector_widget %}
{% spaceless %}
	<style>
	#{{ form.vars.id }} { display: none; }
	.modal#{{ form.vars.id }}_selector a { cursor: pointer; }
	</style>
	<script>
	$(function () {
		var eq_index = null;
		var i = 0;
		for (univ in allUnivList) {
			i++;
			eq_index = (i % 3 >= 1) ? (i % 3 - 1) : 2;
			$('.modal#{{ form.vars.id }}_selector .modal-body>.unstyled>.row-fluid>.span4:eq(' + eq_index + ')').append('<a><li id="' + allUnivList[univ].id + '">' + allUnivList[univ].name + '</li></a>');
			{% if (value) %}
			if (allUnivList[univ].id == {{ value }}) {
				$('#{{ form.vars.id }}_name').val(allUnivList[univ].name);
			}
			{% endif %}
		}
		eq_index = null;
		i = 0;
		
		$('#{{ form.vars.id }}_name').focus(function () {
			$('.modal#{{ form.vars.id }}_selector').modal({ keyboard: false }, 'show');
		});
		$('.modal#{{ form.vars.id }}_selector .row-fluid .span4 li').live('click', function () {
			// a temp function to work with teacher_selector
			if ($(this).attr('id') != $('#{{ form.vars.id }}').val()) {
				$('#{{ form.vars.id }}_name').parent().parent().parent().next().children().children().children('button.teacher-selector').trigger('click');
			}///////////////
			
			$('#{{ form.vars.id }}_name').val($(this)[0].innerHTML);
			$('#{{ form.vars.id }}').val($(this).attr("id"));
			$('.modal#{{ form.vars.id }}_selector').modal('hide');
		});
		$('#{{ form.vars.id }}_name').next('button').click(function () {
			$('#{{ form.vars.id }}_name').val('');
			$('#{{ form.vars.id }}').val('');

			// a temp function to work with teacher_selector
			$('#{{ form.vars.id }}_name').parent().parent().parent().next().children().children().children('button.teacher-selector').trigger('click');
		});
	});
	</script>
	
	<div class="input-append">
	<input class="university-selector" id="{{ form.vars.id }}_name" type="text" />
	<button class="btn university-selector" type="button">清空</button>
	</div>
	
	{{ form_widget(form, { 'attr': {'class': 'university-selector'} }) }}
	
	<div class="modal hide" id="{{ form.vars.id }}_selector">
		<div class="modal-header">
			<a class="close" onclick="$('.modal#{{ form.vars.id }}_selector').modal('hide');">×</a><!-- data-dismiss="modal" -->
			<h3>选择{{ form.vars.label }}</h3>
		</div>
	    <div class="modal-body">
	    	<ul class="unstyled">
		    	<div class="row-fluid">
			    	<div class="span4"></div>
			    	<div class="span4"></div>
			    	<div class="span4"></div>
		    	</div>
	    	</ul>
	    </div>
	    <div class="modal-footer">
		    <a href="#" class="btn" onclick="$('.modal#{{ form.vars.id }}_selector').modal('hide');">关闭</a>
		    <!-- <a href="#" class="btn btn-primary" data-dismiss="modal">选择</a> -->
	    </div>
    </div>
{% endspaceless %}
{% endblock %}

{# custom widget - contest_type_selector #}
{% block contest_type_selector_widget %}
{% spaceless %}
	<style>
	#{{ form.vars.id }} { display: none; }
	.modal#{{ form.vars.id }}_selector a { cursor: pointer; }
	</style>
	<script>
	$(function () {
		for (parentContestType in allContestTypeList) {
			$('.modal#{{ form.vars.id }}_selector .modal-body>.tabbable ul.nav-tabs').append('<li><a href="#' + allContestTypeList[parentContestType].id + '" data-toggle="tab">' + allContestTypeList[parentContestType].name + '</a></li>');

			$('.modal#{{ form.vars.id }}_selector .modal-body>.tabbable .tab-content').append(
				'<div class="tab-pane" id="' + allContestTypeList[parentContestType].id + '">' +
			    	'<ul class="unstyled">' +
				    	'<div class="row-fluid">' +
					    	'<div class="span6"></div>' +
					    	'<div class="span6"></div>' +
				    	'</div>' +
			    	'</ul>' +
			    '</div>'
			);
			
			$('.modal#{{ form.vars.id }}_selector .modal-body>.tabbable ul.nav-tabs li:eq(0)').addClass('active');
			$('.modal#{{ form.vars.id }}_selector .modal-body>.tabbable .tab-content .tab-pane:eq(0)').addClass('active');
			
			var children = allContestTypeList[parentContestType].children;
			var eq_index = null;
			var i = 0;
			for (childContestType in children) {
				i++;
				eq_index = i % 2;
				$('.modal#{{ form.vars.id }}_selector .modal-body>.tabbable .tab-content .tab-pane:eq(' + parentContestType + ') .unstyled>.row-fluid>.span6:eq(' + eq_index + ')').append('<a><li id="' + children[childContestType].id + '">' + children[childContestType].name + '</li></a>');
				{% if (value) %}
				if (children[childContestType].id == {{ value }}) {
					$('#{{ form.vars.id }}_name').val(children[childContestType].name);
				}
				{% endif %}
			}
			eq_index = null;
			i = 0;
		}
		$('#{{ form.vars.id }}_name').focus(function () {
			$('.modal#{{ form.vars.id }}_selector').modal({ keyboard: false }, 'show');
		});
		$('.modal#{{ form.vars.id }}_selector .tab-content .tab-pane .unstyled>.row-fluid>.span6 li').live('click', function () {
			$('#{{ form.vars.id }}_name').val($(this)[0].innerHTML);
			$('#{{ form.vars.id }}').val($(this).attr("id"));
			$('.modal#{{ form.vars.id }}_selector').modal('hide');
		});
		$('#{{ form.vars.id }}_name').next('button').click(function () {
			$('#{{ form.vars.id }}_name').val('');
			$('#{{ form.vars.id }}').val('');
		});
	});
	</script>
	
	<div class="input-append">
	<input id="{{ form.vars.id }}_name" type="text" />
	<button class="btn" type="button">清空</button>
	</div>
	
	{{ form_widget(form) }}
	
	<div class="modal hide" id="{{ form.vars.id }}_selector">
		<div class="modal-header">
			<a class="close" onclick="$('.modal#{{ form.vars.id }}_selector').modal('hide');">×</a><!-- data-dismiss="modal" -->
			<h3>选择{{ form.vars.label }}</h3>
		</div>
	    <div class="modal-body">
	    	<div class="tabbable tabs-left">
		    	<div class="row-fluid">
					<ul class="nav nav-tabs">
						
					</ul>
					<div class="span8">
						<div class="tab-content">
						    
				    	</div>
			    	</div>
			    </div>
		    </div>
	    </div>
	    <div class="modal-footer">
		    <a href="#" class="btn" onclick="$('.modal#{{ form.vars.id }}_selector').modal('hide');">关闭</a>
		    <!-- <a href="#" class="btn btn-primary" data-dismiss="modal">选择</a> -->
	    </div>
    </div>
{% endspaceless %}
{% endblock %}

{# custom widget - teacher_selector #}
{% block teacher_selector_widget %}
{% spaceless %}
	<style>
	#{{ form.vars.id }} { display: none; }
	.modal#{{ form.vars.id }}_selector a { cursor: pointer; }
	</style>
	<script>
	$(function () {
		{% if (value) %}
		for (teacher in allTeacherList) {
			if (allTeacherList[teacher].id == {{ value }}) {
				$('#{{ form.vars.id }}_name').val(
					allTeacherList[teacher].name + '(' + allTeacherList[teacher].username + ' UID=' + allTeacherList[teacher].id + ')'
				);
			}
		}
		{% endif %}
		
		$('#{{ form.vars.id }}_name').focus(function () {
			// a temp function to work with university_selector
			var univ = $(this).parent().parent().parent().prev().children().children('input.university-selector').val();
			
			$('.modal#{{ form.vars.id }}_selector .modal-body>.unstyled>.row-fluid>.span4').html('');
			
			var eq_index = null;
			var i = 0;
			for (teacher in allTeacherList) {
				i++;
				
				// a temp function to work with university_selector
				if (univ != undefined) {
					if (allTeacherList[teacher].university == univ) {
						eq_index = (i % 3 >= 1) ? (i % 3 - 1) : 2;
						$('.modal#{{ form.vars.id }}_selector .modal-body>.unstyled>.row-fluid>.span4:eq(' + eq_index + ')').append('<a><li id="' + allTeacherList[teacher].id + '">' + allTeacherList[teacher].name + '(' + allTeacherList[teacher].username + ' UID=' + allTeacherList[teacher].id + ')' + '</li></a>');
					}
				} else {
					eq_index = (i % 3 >= 1) ? (i % 3 - 1) : 2;
					$('.modal#{{ form.vars.id }}_selector .modal-body>.unstyled>.row-fluid>.span4:eq(' + eq_index + ')').append('<a><li id="' + allTeacherList[teacher].id + '">' + allTeacherList[teacher].name + '(' + allTeacherList[teacher].username + ' UID=' + allTeacherList[teacher].id + ')' + '</li></a>');
				}/////////////////////
			}
			eq_index = null;
			i = 0;
			univ = null;

			$('.modal#{{ form.vars.id }}_selector').modal({ keyboard: false }, 'show');
		});
		
		$('.modal#{{ form.vars.id }}_selector .row-fluid .span4 li').live('click', function () {
			$('#{{ form.vars.id }}_name').val($(this)[0].innerHTML);
			$('#{{ form.vars.id }}').val($(this).attr("id"));
			$('.modal#{{ form.vars.id }}_selector').modal('hide');
		});

		$('#{{ form.vars.id }}_name').next('button').click(function () {
			$('#{{ form.vars.id }}_name').val('');
			$('#{{ form.vars.id }}').val('');
		});
	});
	</script>
	
	<div class="input-append">
	<input class="teacher-selector" id="{{ form.vars.id }}_name" type="text" />
	<button class="btn teacher-selector" type="button">清空</button>
	</div>
	
	{{ form_widget(form, { 'attr': {'class': 'teacher-selector'} }) }}
	
	<div class="modal hide" id="{{ form.vars.id }}_selector">
		<div class="modal-header">
			<a class="close" onclick="$('.modal#{{ form.vars.id }}_selector').modal('hide');">×</a><!-- data-dismiss="modal" -->
			<h3>选择{{ form.vars.label }}</h3>
		</div>
	    <div class="modal-body">
	    	<ul class="unstyled">
		    	<div class="row-fluid">
			    	<div class="span4"></div>
			    	<div class="span4"></div>
			    	<div class="span4"></div>
		    	</div>
	    	</ul>
	    </div>
	    <div class="modal-footer">
		    <a href="#" class="btn" onclick="$('.modal#{{ form.vars.id }}_selector').modal('hide');">关闭</a>
		    <!-- <a href="#" class="btn btn-primary" data-dismiss="modal">选择</a> -->
	    </div>
    </div>
{% endspaceless %}
{% endblock %}

{% block widget_attributes %}
{% spaceless %}
    id="{{ id }}" name="{{ full_name }}"{% if read_only %} disabled="disabled"{% endif %}{#% if required %} required="required"{% endif %#}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}
    {% for attrname,attrvalue in attr %}{{attrname}}="{{attrvalue}}" {% endfor %}
{% endspaceless %}
{% endblock widget_attributes %}

{% block field_errors %}
{% spaceless %}
    {% if errors|length > 0 %}
        {% for error in errors %}
        <span class="help-inline">{{ error.messageTemplate|trans(error.messageParameters, 'validators') }}</span>
        {% endfor %}
    {% endif %}
{% endspaceless %}
{% endblock field_errors %}

{% block field_row %}
{% spaceless %}
<div class="control-group{% if form_errors(form) %} error{% endif %}">
	{{ form_label(form) }}
	
	<div class="controls">
	{{ form_widget(form, { 'value': value }) }}
	{{ form_errors(form) }}
	</div>
</div>
{% endspaceless %}
{% endblock field_row %}